//	Change this : put your own macro here
#ifndef _QUICKYPAD_H_
#define _QUICKYPAD_H_

class QAction;
class QMenu;
class QTextBrowser;

#include <QtCore/QObject>

#include <QTextBrowser>
#include <QRegExp>
#include <QList>

#include "../../src/JuffPlugin.h"

class QuickypadPlugin : public QObject, public JuffPlugin {
Q_OBJECT
Q_INTERFACES(JuffPlugin)
public:
	QuickypadPlugin();
	virtual ~QuickypadPlugin();

	//	initialization/deinitialization
	virtual void init(QObject* handler);
	virtual void deinit(QObject* handler);

	//	plugin name
	virtual QString name() const;
	//	plugin description
	virtual QString description() const;

	//	controls
	//	plugin toolbar
	virtual QToolBar* toolBar() const;
	//	plugin menu that will be added to main menubar
	virtual QMenu* menu() const;
	//	widget that will be put to dock
	virtual QWidget* dockWidget(Qt::DockWidgetArea&) const;
	//	plugin settings widget
	virtual QWidget* settingsPage() const;
	//	apply settings
	virtual void applySettings();

signals:
	void textRequest(QString&);
	void requestCursorPos(int&, int&);
	void insertText(const QString&);
	void moveCursor(int, int);

public slots:
	void view();
	void insertDate();
	void addHeader();

private:
	QAction* viewAction_;
	QAction* dateAction_;
	QAction* headerAction_;
	QMenu* menu_;
	QToolBar* toolBar_;
	QWidget* widget_;
	QTextBrowser* view_;

	// ----
	/**
	 * A Wiki-to-HTML replacement rule.
	 * Holds a regular expression and a destination string. The apply()
	 * method can be used to match the expression to a given string, and
	 * perform text replacement according to the destination string.
	 * @author  Elad Lahav
	 */
	class Rule
	{
	public:
		/**
		 * Class constructor.
		 * @param  pattern  A regular expression specification
		 * @param  dest     The destination string
		 * @param  role     Identifies the semantic role of text matched by
		 *                  this rule
		 */
		Rule(const QString& pattern, const QString& dest, int role)
			: re_(pattern), dest_(dest), role_(role) {}

		/**
		 * Copy constructor.
		 * @param  other  The Rule object to copy
		 */
		Rule(const Rule& other)
			: re_(other.re_), dest_(other.dest_), role_(other.role_) {}

		/**
		 * Replaces an occurance of the regular expression in the given text
		 * with the destination string.
		 * @param  text  The text to match
		 * @return true if a match was found in the text, false otherwise
		 */
		bool apply(QString& text) {
			if (re_.indexIn(text) == -1)
				return false;
		
			text.replace(re_, dest_);
			return true;
		}

		/**
		 * Accessor for the regular expression's pattern text.
		 * @return The pattern string
		 */
		QString pattern() { return re_.pattern(); }
		
		/**
		 * Returns the list of strings captured by parentheses in the regular
		 * expression during the last invocation of apply().
		 * @return The list of captured strings
		 */
		QStringList captureList() { return re_.capturedTexts(); }
		
		/**
		 * Accessor for the role value.
		 * @return The semantic role of text matched by this rule
		 */
		int role() const { return role_; }

	private:
		/**
		 * A regular expression used to match text against this rule.
		 */
		QRegExp re_;
		
		/**
		 * Destination string.
		 * Matched text is replaced with this string (which can contain numbered
		 * place holders for captured text).
		 */
		QString dest_;
		
		/**
		 * A user-supplied value used to identify the semantic role of text
		 * matched by this rule.
		 */
		int role_;
	};
	
	/**
	 * Semantic roles supplied to the rules.
	 */
	enum {
		/** Pre-formatted block. */
		Pre,
		/** Any kind of heading. */
		Title,
		/** Simple text-formatting directives. */
		TextFormat,
		/** List item. */
		ListItem,
		/** Links to other documents. */
		Link,
		/** Blank line. */
		Blank
	};
	
	/**
	 * An ordered list of replacement rules.
	 */
	QList<Rule> reList_;
	
	/**
	 * Environment blocks.
	 */
	enum Environment {
		/** None (e.g., titles) */
		None,
		/** Paragraphs (<p>...</p>) */
		Paragraph,
		/** Lists (<ul>...</ul> or <ol>...</ol>)*/
		List,
		/** Preformatted blocks (<pre>...</pre>) */
		PreBlock
	};
		
	/**
	 * The current environment block.
	 * Changes to this variable trigger the opening or closing of blocks.
	 */
	Environment env_;
	
	/**
	 * Holds a list item string (a combination of '*' and '#') that identifies
	 * the current list level.
	 * Used for opening and closing list environments, whenever the new list
	 * level does not match the current one.
	 */
	QString curListLevel_;

	QString formatLine(const QString&);
	QString adjustListLevel(const QString&);
};

#endif
